package com.example.lhsama.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

@Data
public class User implements UserDetails {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String userName;
    private String password;
    private int enable;
    private int deleted;
    private String userDesc;

    @TableField(exist = false)
    private String enableStr;
    @TableField(exist = false)
    private Collection<? extends GrantedAuthority> Authorities;
    @TableField(exist = false)
    private boolean isAccountNonExpired = true;
    @TableField(exist = false)
    private boolean isAccountNonLocked;
    @TableField(exist = false)
    private boolean isCredentialsNonExpired = true;
    @TableField(exist = false)
    private List<Role> roles;


    public String getEnableStr() {
        return enable==1?"开启":"禁用";
    }

    /**
     * 返回授予用户的权限集合，不能返回null
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.Authorities;
    }

    /**
     * 用户的密码
     */
    @Override
    public String getPassword() {
        return this.password;
    }

    /**
     * 返回用户名，用户名也不能为空
     */
    @Override
    public String getUsername() {
        return this.userName;
    }

    /**
     * 用户是否过期，没有过期就返回true
     */
    @Override
    public boolean isAccountNonExpired() {
        return this.isAccountNonExpired;
    }

    /**
     * 用户是否被锁定，锁定返回true。
     */
    @Override
    public boolean isAccountNonLocked() {
        return enable==1?true:false;
    }

    /**
     * 用户凭证是否可用，可用返回true
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return this.isCredentialsNonExpired;
    }

    /**
     * 用户是否启用了，启用了返回true
     */
    @Override
    public boolean isEnabled() {
        //1，开启 0，关闭
        if (enable == 1){
            return true;
        }
        return false;
    }

}
